home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PASCTXT.ZIP / CHAP9.TXT < prev    next >
Text File  |  1988-01-15  |  21KB  |  455 lines

  1.  
  2.                            CHAPTER 9 - Records
  3.  
  4.  
  5.              We  come  to the grandaddy of all  data  structures  in
  6.         Pascal,  the  record.  A record is composed of a  number  of
  7.         variables  any of which can be of any predefined data  type,
  8.         including  other records.  Rather than spend time trying  to
  9.         define  a  record  in detail, lets go  right  to  the  first
  10.         example program, SMALLREC.  This is a program using nonsense
  11.         data that will illustrate the use of a record.
  12.  
  13.                            A VERY SIMPLE RECORD
  14.  
  15.              There is only one entry in the type declaration part of
  16.         the  program,  namely  the record  identified  by  the  name
  17.         Description.   The record is composed of three  fields,  the
  18.         Year,  Model, and Engine variables.  Notice that  the  three
  19.         fields  are  each of a different type, indicating  that  the
  20.         record  can be of mixed types.  You have a complete  example
  21.         of  the way a record is defined before you.  It is  composed
  22.         of  the identifier Description, the reserved word  "record",
  23.         the  list of elements, and followed by end.  This is one  of
  24.         the  places  in  Pascal  where an  end  is  used  without  a
  25.         corresponding begin.  Notice that this only defines a  type,
  26.         it  does not define any variables.  That is done in the  var
  27.         declaration  where the variable Cars is defined to  have  10
  28.         complete  records  of the type  Description.   The  variable
  29.         Cars[1]  has three components, Year, Model, and Engine,  and
  30.         any  or  all of these components can be used to  store  data
  31.         pertaining to Cars[1].
  32.  
  33.              When  assigning  data  to  the  variable  Cars[1],  for
  34.         example, there are actually three parts to the variable,  so
  35.         we  use  three assignment statements, one for  each  of  the
  36.         sub-fields.   In  order  to assign  values  to  the  various
  37.         sub-fields,  the variable name is followed by the  sub-field
  38.         name   with  a  separating  period.    The   "var.sub_field"
  39.         combination is a variable name.
  40.  
  41.              Keep  in  mind  that  Cars[1]  is  a  complete   record
  42.         containing three variables, and to assign or use one of  the
  43.         variables,  you  must  designate  which  sub-field  you  are
  44.         interested  in.  See the program where the three fields  are
  45.         assigned meaningless data for illustration.  The Year  field
  46.         is  assigned an integer number varying with  the  subscript,
  47.         all  Model fields are assigned the name Duesenburg, and  all
  48.         Engine  variables  are assigned the value V8.  In  order  to
  49.         further  illustrate that there are actually 30 variables  in
  50.         use  here, a few are changed at random in lines  20  through
  51.         24,  being  very careful to maintain the required  types  as
  52.         defined  in  the  type  declaration  part  of  the  program.
  53.         Finally,  all  ten  composite variables,  consisting  of  30
  54.         actual variables in a logical grouping are printed out using
  55.         the same "var.sub-field" notation described above.
  56.  
  57.  
  58.                                   Page 51
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                            CHAPTER 9 - Records
  69.  
  70.  
  71.  
  72.              If  the preceding description of a record is not  clear
  73.         in  your  mind,  review  it very  carefully.   It's  a  very
  74.         important concept in Pascal, and you won't have a hope of  a
  75.         chance  of understanding the next example until this one  is
  76.         clear.
  77.  
  78.              Be  sure to compile and run SMALLREC so you  can  study
  79.         the output.
  80.  
  81.                               A SUPER RECORD
  82.  
  83.              Examine  the  Pascal  example file BIGREC  for  a  very
  84.         interesting  record.   First  we have  a  constant  defined.
  85.         Ignore  it  for the moment, we will come back to  it  later.
  86.         Within  the type declaration we have three records  defined,
  87.         and  upon close examination, you will notice that the  first
  88.         two  records are included as part of the definition  of  the
  89.         third  record.   The record identified as  Person,  actually
  90.         contains 9 variable definitions, three within the  Full_Name
  91.         record, three of its own, and three within the Date  record.
  92.         This is a type declaration and does not actually define  any
  93.         variables, that is done in the var part of the program.
  94.  
  95.              The  var  part of the program  defines  some  variables
  96.         beginning with the array of Friend containing 50 (because of
  97.         the  constant definition in the const part) records  of  the
  98.         user defined type, Person.  Since the type Person defines  9
  99.         fields,  we have now defined 9 times 50 = 450  separate  and
  100.         distinct   variables,  each  with  its  own  defined   type.
  101.         Remember  that Pascal is picky about assigning data  by  the
  102.         correct  type.  Each of the 450 separate variables  has  its
  103.         own type associated with it, and the compiler will  generate
  104.         an  error  if you try to assign any of those  variables  the
  105.         wrong  type of data.  Since Person is a type definition,  it
  106.         can be used to define more than one variable, and in fact it
  107.         is  used again to define three more records,  Self,  Mother,
  108.         and  Father.   These three records are each  composed  of  9
  109.         variables,  so  we  have  27 more  variables  which  we  can
  110.         manipulate within the program.  Finally we have the variable
  111.         Index defined as a simple byte type variable.
  112.  
  113.                     HOW TO MANIPULATE ALL OF THAT DATA
  114.  
  115.              In the program we begin by assigning data to all of the
  116.         fields of Self in lines 31 through 43.  Examining the  first
  117.         three   statements   of  the  main  program,  we   see   the
  118.         construction  we learned in the last example  program  being
  119.         used, namely the period between descriptor fields.  The main
  120.         record  is  named Self, and we are interested in  the  first
  121.         part of it, specifically the Name part of the Person record.
  122.  
  123.  
  124.                                   Page 52
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                            CHAPTER 9 - Records
  135.  
  136.  
  137.         Since the Name part of the Person record is itself  composed
  138.         of  three parts, we must designate which component of it  we
  139.         are  interested  in.  Self.Name.First_Name is  the  complete
  140.         description  of  the first name of Self and is used  in  the
  141.         assignment  statement  in line 31 where it is  assigned  the
  142.         name  of "Charley".  The next two fields are handled in  the
  143.         same way and are self explanatory.
  144.  
  145.                         WHAT IS THE WITH STATEMENT?
  146.  
  147.              Continuing on to the fourth field, the City, there  are
  148.         only two levels required because City is not another  record
  149.         definition.   The  fourth  field  is  therefore   completely
  150.         defined by Self.City.  Notice the "with Self do"  statement.
  151.         This is a shorthand notation used with record definitions to
  152.         simplify coding.  From the begin in line 34 to the  matching
  153.         end  in  line 43, any variables within the Self  record  are
  154.         used  as  though they had a "Self." in front  of  them.   It
  155.         greatly  simplifies  coding to be able to omit  the  leading
  156.         identifier  within the with section of code.  You  will  see
  157.         that  City,  State, and Zipcode are easily  assigned  values
  158.         without further reference to the Self variable.  When we get
  159.         to the Day part of the birthday, we are back to three levels
  160.         and  the complete definition is Self.Birthday.Day  but  once
  161.         again,  the  "Self."  part is taken  care  of  automatically
  162.         because we are still within the "with Self do" area.
  163.  
  164.              To  illustrate the with statement further,  another  is
  165.         introduced  in line 39, "with Birthday do", and an  area  is
  166.         defined  by  the begin end pair which extends from  line  39
  167.         through line 42.  Within this area both leading  identifiers
  168.         are  handled automatically to simplify coding, and Month  is
  169.         equivalent  to  writing  Self.Birthday.Month  if  both  with
  170.         statements were removed.
  171.  
  172.                HOW FAR DOWN CAN YOU NEST THE WITH STATEMENT?
  173.  
  174.              You  may  be wondering how many levels of  nesting  are
  175.         allowed in record definitions.  There doesn't appear to be a
  176.         limit  according to the Pascal definition, but we do  get  a
  177.         hint at how far it is possible to go.  In TURBO Pascal,  you
  178.         are  allowed to have with statements nested to nine  levels,
  179.         and  it  would be worthless to nest with  statements  deeper
  180.         than  the  level  of records.  Any  program  requiring  more
  181.         levels  than nine is probably far beyond the scope  of  your
  182.         programming  ability,  and mine, for a  long  time.   Pascal
  183.         implementations other than TURBO Pascal probably have  their
  184.         own with statement nesting limitation.  Check your reference
  185.         manual.
  186.  
  187.  
  188.  
  189.  
  190.                                   Page 53
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                            CHAPTER 9 - Records
  201.  
  202.  
  203.              After  assigning a value to Year, the entire record  of
  204.         Self  is defined, all nine variables.  It should be  pointed
  205.         out  that  even  though Self is composed  of  nine  separate
  206.         variables,  it  is  proper to call Self  a  variable  itself
  207.         because it is a record variable.
  208.  
  209.                         SUPER-ASSIGNMENT STATEMENTS
  210.  
  211.              The  statement  in line 45, "Mother := Self;"  is  very
  212.         interesting.  Since both of these are records, both are  the
  213.         same type of record, and both therefore contain 9 variables,
  214.         Pascal  is  smart enough to recognize that, and  assign  all
  215.         nine values contained in Self to the corresponding variables
  216.         of  Mother.   So after one statement,  the  record  variable
  217.         Mother  is  completely defined.  The statement  in  line  46
  218.         assigns the same values to the nine respective variables  of
  219.         Father,  and  the  next  two  lines  assign  all  50  Friend
  220.         variables  the same data.  By this point in the program,  we
  221.         have  therefore generated 450 + 27 = 477 separate pieces  of
  222.         data so far in this program.  We could print it all out, but
  223.         since  it  is nonsense data, it would only  waste  time  and
  224.         paper.  Lines 49 through 52 write out three sample pieces of
  225.         the data for your inspection.
  226.  
  227.                          WHAT GOOD IS ALL OF THIS
  228.  
  229.              It  should  be obvious to you that  what  this  program
  230.         does,  even though the data is nonsense, appears to  be  the
  231.         beginning  of a database management system, which indeed  it
  232.         is.   Instead  of assigning nonsense data, a list  could  be
  233.         read  in  and  stored  for  manipulation.   It  is  a  crude
  234.         beginning,  and has a long way to go to be useful,  but  you
  235.         should see a seed for a useful program.
  236.  
  237.              Now to go back to the const in line 4 as promised.  The
  238.         number of friends was defined as 50 and used for the size of
  239.         the  array and in the assignment loop in line 47.   You  can
  240.         now  edit  this  number and see how big  this  database  can
  241.         become on your computer.  If you are using TURBO Pascal, you
  242.         will  be limited to slightly more than 1000 because  of  the
  243.         64K  limitation of an executable program, and the fact  that
  244.         all  of  this data is stored within that 64K  boundary.   It
  245.         should  be  noted  that TURBO Pascal 4.0  allows  a  program
  246.         larger than 64K but still places a limitation of 64K on each
  247.         compilation  unit.  See how big you can make the  number  of
  248.         friends  before you get the memory overflow  message.   Keep
  249.         the  number  in mind because when we get to the  chapter  on
  250.         Pointers  and  Dynamic  Allocation, you will  see  a  marked
  251.         increase  in allowable size, especially if you have a  large
  252.         amount of RAM installed in your computer.
  253.  
  254.  
  255.  
  256.                                   Page 54
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                            CHAPTER 9 - Records
  267.  
  268.  
  269.                              A VARIANT RECORD
  270.  
  271.              If any part of this chapter is still unclear, it  would
  272.         be good for you to go back and review it at this time.   The
  273.         next  example  will  really  tax  your  mind  to  completely
  274.         understand  it,  especially  if the prior  material  is  not
  275.         clear.
  276.  
  277.              Examine  the Pascal program VARREC for an example of  a
  278.         program with a variant record definition.  In this  example,
  279.         we  first define a scalar type, namely  Kind_Of_Vehicle  for
  280.         use  within  the  record.  Then we have  a  record  defining
  281.         Vehicle, intended to define several different vehicles, each
  282.         with  different  kinds  of data.  It would  be  possible  to
  283.         define all variables for all types of vehicles, but it would
  284.         be  a waste of storage space to define the number  of  tires
  285.         for a boat, or the number of propeller blades used on a  car
  286.         or  truck.   The  variant record lets  us  define  the  data
  287.         precisely  for  each vehicle without  wasting  data  storage
  288.         space.
  289.  
  290.                            WHAT IS A TAG-FIELD?
  291.  
  292.              In  the  record  definition we have  the  usual  record
  293.         header  followed  by  three variables defined  in  the  same
  294.         manner  as  the records in the last  two  example  programs.
  295.         Then  we  come  to  the  case  statement.   Following   this
  296.         statement,  the  record is different for each  of  the  four
  297.         types  defined  in the associated  scalar  definition.   The
  298.         variable  What_Kind  is  called the tag-field  and  must  be
  299.         defined  as  a scalar type prior to the  record  definition.
  300.         The  tag-field  is  used to select  the  variant,  when  the
  301.         program uses one of the variables of this record type.   The
  302.         tag-field  is followed by a colon and its  type  definition,
  303.         then the reserved word "of".  A list of the variants is then
  304.         given,  with each of the variants having the  variables  for
  305.         its particular case defined.  The list of variables for  one
  306.         variant is called the field list.
  307.  
  308.              A  few rules are in order at this point.  The  variants
  309.         do  not  have to have the same number of variables  in  each
  310.         field  list,  and in fact, one or more of the  variants  may
  311.         have no variables at all in its variant part.  If a  variant
  312.         has  no variables, it must still be defined with a  pair  of
  313.         empty  parentheses followed by a semi-colon.  All  variables
  314.         in  the  entire variant part must have  unique  names.   The
  315.         three variables, Wheels, Tires, and Tyres, all mean the same
  316.         thing  to  the  user, but they must  be  different  for  the
  317.         compiler.   You may use the same identifiers again in  other
  318.         records  and  for  simple variables  anywhere  else  in  the
  319.         program.   The Pascal compiler can tell which  variable  you
  320.  
  321.  
  322.                                   Page 55
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                            CHAPTER 9 - Records
  333.  
  334.  
  335.         mean by its context.  Using the same variable name should be
  336.         discouraged  as  bad  programming practice  because  it  may
  337.         confuse  you  or another person trying  to  understand  your
  338.         program at a later date.
  339.  
  340.              The  final rule is that the variant part of the  record
  341.         must  be the last part of it, and in fact, the last part  of
  342.         any  or all variants can itself have a variant part  to  it.
  343.         That  is  getting pretty advanced for our level  of  use  of
  344.         Pascal at this time however.
  345.  
  346.                          USING THE VARIANT RECORD
  347.  
  348.              We properly define four variables with the record  type
  349.         Vehicle in line 22 and go on to examine the program itself.
  350.  
  351.              We  begin  by  defining one of our  variables  of  type
  352.         Vehicle,  namely the variable named Ford.  The  seven  lines
  353.         assigning  values to Ford are similar to the prior  examples
  354.         with  the exception of line 28.  In that line the  tag-field
  355.         which  selects the particular variant used is set  equal  to
  356.         the  value  Truck,  which  is a  scalar  definition,  not  a
  357.         variable.  This means that the variables named Motor, Tires,
  358.         and Payload are available for use with the record Ford,  but
  359.         the  variables  named Wheels, Engine, Tyres,  etc.  are  not
  360.         available in the record named Ford.
  361.  
  362.              Next, we will define the record Sunfish as a Boat,  and
  363.         define all of its variables in lines 33 through 41.  All  of
  364.         Sunfish's variables are defined but in a rather random order
  365.         to illustrate that they need not be defined in a  particular
  366.         order.  You should remember the with statement from the last
  367.         example program.
  368.  
  369.              To go even further in randomly assigning the  variables
  370.         to  a record, we redefine Ford as having an Engine which  it
  371.         can  only  have  if it is a car.  This is one  of  the  fine
  372.         points  of  the  Pascal record.  If you assign  any  of  the
  373.         variant  variables, the record is changed to  that  variant,
  374.         but  it  is  the programmers responsibility  to  assign  the
  375.         correct  tag-field  to  the  record,  not  Pascal's.    Good
  376.         programming practice would be to assign the tag-field before
  377.         assigning  any of the variant variables.  The  remainder  of
  378.         the Ford variables are assigned to complete that record, the
  379.         non-variant part remaining from the last assignment.
  380.  
  381.              The  variable  Mac  is now set equal  to  the  variable
  382.         Sunfish  in  line 48.  All variables within the  record  are
  383.         copied to Mac including the tag-field, making Mac a Boat.
  384.  
  385.  
  386.  
  387.  
  388.                                   Page 56
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                            CHAPTER 9 - Records
  399.  
  400.  
  401.                   NOW TO SEE WHAT WE HAVE IN THE RECORDS
  402.  
  403.              We have assigned Ford to be a car, and two boats exist,
  404.         namely Sunfish and Mac.  Since Schwinn was never defined, it
  405.         has  no data in it, and is at this point useless.  The  Ford
  406.         tag-field has been defined as a car, so it should be true in
  407.         the  if statement, and the message in line 51 should  print.
  408.         The Sunfish is not a bicycle, so it will not print.  The Mac
  409.         has  been  defined  as  a  boat  in  the  single  assignment
  410.         statement,  so  it will print a message with  an  indication
  411.         that  all of the data in the record was transferred  to  its
  412.         variables.
  413.  
  414.              Even  though  we can make  assignment  statements  with
  415.         records, they cannot be used in any mathematical  operations
  416.         such  as addition, or multiplication.  They are simply  used
  417.         for  data storage.  It is true however, that the  individual
  418.         elements  in  a  record  can be  used  in  any  mathematical
  419.         statements legal for their respective types.
  420.  
  421.              One other point should be mentioned.  The tag-field can
  422.         be completely eliminated resulting in a "free union" variant
  423.         record.   This  is  possible  because  Pascal,  as  you  may
  424.         remember  from above, will automatically assign the  variant
  425.         required when you assign data to one of the variables within
  426.         a variant.  This is the reason that all variables within any
  427.         of  the  variants must have unique names.   The  free  union
  428.         record  should be avoided in your early programming  efforts
  429.         because you cannot test a record to see what variant it  has
  430.         been assigned to it.  It is definitely an advanced technique
  431.         in Pascal.
  432.  
  433.              Be sure you compile and run VARREC and study the output
  434.         until you understand it completely.
  435.  
  436.                            PROGRAMMING EXERCISE
  437.  
  438.         1.  Write  a simple program with a record to store the names
  439.             of five of your friends and display the names.
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.                                   Page 57
  455.